{include file="public/head" /}
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <div class="layui-tab">
        <ul class="layui-tab-title">
            <li class="layui-this">调查问卷列表</li>
        </ul>
    </div>
    <div class="layui-col-md12">
        <div class="layui-inline">
            <input type="text" placeholder="按ID,问卷名搜索" class="layui-input" id="keyword">
        </div>
        <button class="layui-btn" id="search">搜索</button>
        <button class="layui-btn layui-btn-danger" id="reset">重置</button>

        <table id="table" lay-filter="table"></table>
        <script type="text/html" id="operation">
            {{#  if(d.status > 0){ }}
            <a class="layui-btn layui-btn-xs" lay-event="stop">结束</a>
            {{#  } }}
            <a class="layui-btn layui-btn-success layui-btn-xs" lay-event="detail">查看详情</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
</div>
<script>
    var domain = '{$domain}';
    layui.use(['element','table','form'], function(){
        var table = layui.table,layer = layui.layer;
        var form = layui.form;
        //表格加载
        table.render({
            elem: '#table'
            ,height: 450
            ,url: '/admin/Survey/getList'
            ,page: true
            ,cols: [[
                {field: 'id', title: 'ID', sort: true, fixed: 'left', width:40}
                ,{field: 'title', title: '问卷名称', width:180}
                ,{field: 'answer_count', title: '已作答份数', width:100}
                ,{field: 'status', title: '状态', width:80, templet: function(d){
                    return d.status==0 ? '已结束' : '进行中';
                }}
                ,{field: 'create_time', title: '创建时间', width:170}
                ,{field: 'end_time', title: '结束时间', width:170, templet: function(d){
                    return d.status==0 ? d.end_time : '';
                }}
                ,{title: '问卷链接', width:170, templet: function(d){
                    return domain+'/user/index/index/id/'+d.id;
                }}
                ,{title:'操作', toolbar: '#operation', width:220}
            ]]
        });
        //监听表格操作按钮
        table.on('tool(table)', function(obj){
            var data = obj.data;
            var id = data.id;//操作的记录id
            //console.log(obj);
            if(obj.event === 'stop'){
                //结束问卷
                layer.confirm('确认要结束吗?', function(index){
                    $.post("{:url('admin/Survey/stop')}",{id:id},function(res){
                        if(res.code==0){
                            layer.msg(res.msg, {time: 2000, icon:5});
                        }else{
                            table.reload('table', {
                                url: "/admin/survey/getList"
                                ,where: {}
                            });
                            layer.msg(res.msg, {time: 1500, icon:6});
                        }
                    },"json");
                });
            }else if(obj.event === 'del'){
                //删除问卷
                layer.confirm('确认要删除吗?', function(index){
                    $.post("{:url('admin/Survey/delete')}",{id:id},function(res){
                        if(res.code==0){
                            layer.msg(res.msg, {time: 2000, icon:5});
                        }else{
                            obj.del();//前端删除
                            table.reload('table', {
                                url: "/admin/survey/getList"
                                ,where: {}
                            });
                            layer.msg(res.msg, {time: 1500, icon:6});
                        }
                    },"json");
                });
            }else if(obj.event === 'detail'){
                //查看详情
                layer.open({
                    type: 2,
                    title: '问卷详情',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['90%', '90%'],
                    content: "/admin/survey/detail/id/"+id
                });
            }else if(obj.event === 'copyUrl'){
                oInput.select(); // 选择对象
                document.execCommand("Copy"); // 执行浏览器复制命令
                window.clipboardData.setData("Text",domain+'/user/index/index/id/'+id);
            }
        });

        //任务名搜索
        $("#search").click(function () {
            //获取搜索的关键字
            var keyword = $("#keyword").val();
            if(keyword==''){return false;}
            var url = "/admin/Survey/getList?keyword="+keyword;
            table.reload('table', {
                url: url
                ,where: {}
            });
        });
        //重置搜索
        $("#reset").click(function () {
            $("#keyword").val('');
            //表格重载
            table.reload('table', {
                url: "/admin/Survey/getList?keyword="
                ,where: {}
            });
        });
    });
</script>
</body>
</html>